package com.dgl.sdk.util;

import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;

public class DBStore {

	private static FinalDb finalDb;
	
	private DBStore(){}
	
	/**
	 * 使用前必须init
	 * @param context
	 */
	public static void init(Context context, String dbName){
		finalDb = FinalDb.create(context, dbName);
	}
	
	public static void save(Object entity, String tableName){
		if(finalDb!= null){
			finalDb.saveWithTableName(entity, tableName);
		}
	}
	
	public static  <T> void saveList(List<T> list, String tableName){
		if(finalDb!= null){
			finalDb.saveListWithTableName(list, tableName);
		}
	}
	
	public static <T> T read(Class<T> clazz,String tableName, String strWhere){
		return finalDb.findByWhereWithTableName(clazz, tableName, strWhere);
	}
	
	
	public static <T> List<T> readAll(Class<T> clazz,String tableName){
		return finalDb.findAllWithTableName(clazz, tableName);
	}

    public static void delete(Object entity, String tableName){
        if(finalDb!= null){
            finalDb.deleteWithTableName(entity, tableName);
        }
    }
}
